package cycling.taxi.com.ui.activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.bean.HistoryInfo;
import cycling.taxi.com.bean.PositionInfo;
import cycling.taxi.com.parse.ParseTraject;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;
import cycling.taxi.com.zxing.android.Intents;


/**
 * 里程
 * Created by Administrator on 2017/5/3.
 */
public class MileageAty extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener,RequestCallback {

    private ListView mMileageLv;

    List<Integer> mList;
    private List<HistoryInfo> data;
    private ProgressBar pbs;
    private ImageView back;
    private int i;
    private TextView none;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_mileage);
        pbs=(ProgressBar) findViewById(R.id.pbs);
        none=(TextView)findViewById(R.id.nonetr);
        pbs.setVisibility(View.VISIBLE);
        ExitApplication.getInstance().addActivity(this);
        initUI();
        initData();
        DrawTrajectory();
    }

    private void initData() {
        back =(ImageView)findViewById(R.id.back);
        back.setOnClickListener(this);
       // mMileageLv.setOnItemClickListener(this);
      /*  TrajectoryAdapter trajectoryAdapter = null;
        if (trajectoryAdapter == null) {
            trajectoryAdapter = new TrajectoryAdapter();
        }*/

    //    mMileageLv.setAdapter(trajectoryAdapter);
     //   mMileageLv.setOnItemClickListener(this);


    }
    private void DrawTrajectory() {
        //获取服务器数据
        String mobile= SPUtil.getInstance().getData("phone");
        String password= SPUtil.getInstance().getData("password");
        RequestParams params = new RequestParams.Builder()
                .putParam("mobile",mobile)
                .putParam("password", MD5Util.MD5Encode(password))
                .putParam("beanJson",SPUtil.getInstance().getData("beanJson"))
                .build();

        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Urls.getTrajectoryList();//轨迹的url路径
        RequestUtil.requestData(requestQueue,
                IConstants.TRAJECTORY,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);

    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent trajectory = new Intent(MileageAty.this, TrajectoryActivity.class);
        List<HistoryInfo> historyInfos=new ArrayList<HistoryInfo>();

        //获取当前选择的值
        Adapter adpter=parent.getAdapter();
        HistoryInfo item=(HistoryInfo) adpter.getItem(position);//拿到当前数据值并强转   adpter.getItem(i)即为当前数据对象
        String json=new Gson().toJson(item,HistoryInfo.class);
        SPUtil.getInstance().setData("history",json);
        LogUtil.e("gogogo"+item.getTime_durtion());
        startActivity(trajectory);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.back){
            finish();
        }
    }

    @Override
    public void requestSuccess(RequestBean bean, Config config) {
        LogUtil.e("requestSuccess数据="+bean.beanJson);
        if(bean.getStatusCode()==2){
            try {
                data= ParseTraject.historyInfos(bean.beanJson.toString());
                mMileageLv.setVisibility(View.VISIBLE);
                mMileageLv.setAdapter(new ListAdapter());
                mMileageLv.setOnItemClickListener(this);
                pbs.setVisibility(View.GONE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            pbs.setVisibility(View.GONE);
            none.setVisibility(View.VISIBLE);
        }


        LogUtil.e("成功");
    }

    @Override
    public void requestFail(RequestBean bean, Config config) {
        LogUtil.e("失败");
        pbs.setVisibility(View.GONE);
        none.setVisibility(View.VISIBLE);
        ToastUtil.getInstance().toast("查询轨迹失败");
    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {
        LogUtil.e("Token失效");
    }
    public class ListAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if (convertView==null) {
                convertView = View.inflate(MileageAty.this,R.layout.item_mileage, null);
            }
            HistoryInfo historyInfo = data.get(position);
            holder = new ViewHolder();
            holder.time_durtion = (TextView) convertView.findViewById(R.id.time_durtion);
            holder.tvs = (TextView) convertView.findViewById(R.id.tvs);
            holder.tv_licheng=(TextView)convertView.findViewById(R.id.tv_licheng);
            holder.tv_yongshi=(TextView)convertView.findViewById(R.id.tv_yongshi);
            holder.tv_pju=(TextView)convertView.findViewById(R.id.tv_pju);
            holder.time_durtion.setText(historyInfo.getTime_durtion());
            holder.tvs.setText(historyInfo.getTime());
            holder.tv_licheng.setText(historyInfo.getLicheng());
            holder.tv_yongshi.setText(historyInfo.getUseTime());
            holder.tv_pju.setText(historyInfo.getSpeed());


            convertView.setTag(holder);

            return convertView;
        }
    }

    static class ViewHolder {
        TextView tvs;
        TextView time_durtion;
        TextView tv_licheng;
        TextView tv_yongshi;
        TextView tv_pju;

    }
   /* public class TrajectoryAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            LogUtil.e(String.valueOf(data.size()));
            return data.size();

        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView=View.inflate(MileageAty.this,R.layout.item_mileage,null);
                HistoryInfo historyInfo = data.get(position);

                TextView tv=(TextView)convertView.findViewById(R.id.tvs);
                TextView time_durtion=(TextView)convertView.findViewById(R.id.time_durtion);
                TextView tv_licheng=(TextView)convertView.findViewById(R.id.tv_licheng);
                TextView tv_yongshi=(TextView)convertView.findViewById(R.id.tv_yongshi);
                TextView tv_pju=(TextView)convertView.findViewById(R.id.tv_pju);
                tv.setText(historyInfo.getTime());
                time_durtion.setText(historyInfo.getTime_durtion());
                tv_licheng.setText(historyInfo.getLicheng());
                tv_yongshi.setText(historyInfo.getUseTime());
                tv_pju.setText(historyInfo.getSpeed());
            }
            return convertView;
        }
    }*/

    private void initUI() {
        mMileageLv = (ListView) findViewById(R.id.aty_mileage_lv);



    }
}
